package com.example;

import com.example.model.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonParser {
    public static void main(String[] args) {
        System.out.println("=== JSON解析示例 ===");
        
        // 创建Gson实例
        Gson gson = new GsonBuilder()
            .setPrettyPrinting()  // 格式化输出
            .create();
        
        // 创建用户对象
        User user = new User("张三", 30, "zhangsan@example.com");
        
        // 对象转JSON
        String json = gson.toJson(user);
        System.out.println("对象转JSON:\n" + json);
        
        // JSON转对象
        User parsedUser = gson.fromJson(json, User.class);
        System.out.println("\nJSON转对象:");
        System.out.println("姓名: " + parsedUser.getName());
        System.out.println("年龄: " + parsedUser.getAge());
        System.out.println("邮箱: " + parsedUser.getEmail());
    }
}